#### TABLES B.1-B.4: COVARIATE BALANCE
#### Balance tables for all studies

rm(list = ls())
source("./2_code/00_setup.R")

#### HELPER FUNCTION ####

create_balance_table <- function(data, variables, variable_names) {
  
  means_list <- list()
  p_values <- numeric()
  counts <- data[, .N, by = treat]
  
  # Calculate means and p-values for each variable
  for (var in variables) {
    group_means <- data[, .(mean = mean(get(var), na.rm = TRUE)), by = treat]
    means_list[[var]] <- setnames(group_means, old = "mean", new = var)
    
    result <- aov(get(var) ~ treat, data = data)
    anova_table <- summary(result)
    p_value <- anova_table[[1]]["treat", "Pr(>F)"]
    p_values <- c(p_values, p_value)
  }
  
  # Build table matrix
  table_matrix <- matrix(nrow = length(variables) + 1, ncol = 5)
  colnames(table_matrix) <- c("Variable", "Control", "Humanitarian", "Economic", "P-value")
  
  for (i in 1:length(variables)) {
    var <- variables[i]
    table_matrix[i, 1] <- variable_names[i]
    
    control_mean <- means_list[[var]][means_list[[var]]$treat == "control", 2]
    humanitarian_mean <- means_list[[var]][means_list[[var]]$treat == "humanitarian", 2]
    economic_mean <- means_list[[var]][means_list[[var]]$treat == "economic", 2]
    
    table_matrix[i, 2] <- as.character(round(control_mean, 4))
    table_matrix[i, 3] <- as.character(round(humanitarian_mean, 4))
    table_matrix[i, 4] <- as.character(round(economic_mean, 4))
    table_matrix[i, 5] <- as.character(round(p_values[i], 4))
  }
  
  # Add N row
  table_matrix[length(variables) + 1, 1] <- "N"
  table_matrix[length(variables) + 1, 2] <- as.character(counts[treat == "control", N])
  table_matrix[length(variables) + 1, 3] <- as.character(counts[treat == "humanitarian", N])
  table_matrix[length(variables) + 1, 4] <- as.character(counts[treat == "economic", N])
  
  return(xtable::xtable(table_matrix))
}


#### TABLE B.1: STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)

variables <- c("edad", "male", "worked_dummy", "secondary", "prejudice_baseline", "single")
variable_names <- c("Age", "Male", "Employed", "Secondary Education or More", 
                    "Baseline Feelings of Migrants", "Single")

table_B1 <- create_balance_table(data, variables, variable_names)

latex_output <- print(table_B1, type = "latex", floating = FALSE, 
                      include.rownames = FALSE, print.results = FALSE)
writeLines(latex_output, paste0(tables_path, "table_B1.tex"))


#### TABLE B.2: STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)

variables <- c("edad", "male", "single", "worked_dummy", "secondary", "prejudice_baseline")
variable_names <- c("Age", "Male", "Single", "Employed", "Secondary Education or More", 
                    "Baseline Feelings of Migrants")

table_B2 <- create_balance_table(data2, variables, variable_names)

latex_output <- print(table_B2, type = "latex", floating = FALSE, 
                      include.rownames = FALSE, print.results = FALSE)
writeLines(latex_output, paste0(tables_path, "table_B2.tex"))


#### TABLE B.3: STUDY 3 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)

variables <- c("edad", "male", "urban", "worked_dummy", "secondary", "ideology_c", "information")
variable_names <- c("Age", "Male", "Urban", "Employed", "Secondary Education or More", 
                    "Left Right Ideology [-2,2]", "Seek Political Information [0=Never,4=Daily]")

table_B3 <- create_balance_table(data3, variables, variable_names)

latex_output <- print(table_B3, type = "latex", floating = FALSE, 
                      include.rownames = FALSE, print.results = FALSE)
writeLines(latex_output, paste0(tables_path, "table_B3.tex"))


#### TABLE B.4: STUDY 3 WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)

variables <- c("edad", "male", "urban", "worked_dummy", "secondary", "ideology_c", "information")
variable_names <- c("Age", "Male", "Urban", "Employed", "Secondary Education or More", 
                    "Left Right Ideology [-2,2]", "Seek Political Information [0=Never,4=Daily]")

table_B4 <- create_balance_table(data3b, variables, variable_names)

latex_output <- print(table_B4, type = "latex", floating = FALSE, 
                      include.rownames = FALSE, print.results = FALSE)
writeLines(latex_output, paste0(tables_path, "table_B4.tex"))
